﻿/************************************
 * 作者：eric.cao
 * 
 * 该类名称：翻页控件一
 * 
 * 
 * *********************************/
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

[assembly: WebResource("Common.Controls.Pagination.Pagination.css", "css")]
namespace BaseSet.Entity.Common.Controls
{
    public class Pagination : WebControl, INamingContainer, IPostBackEventHandler
    {
        public enum PaginationMode
        {
            Get,
            Post
        }

        #region = 1 Count =
        /// <summary>
        /// 内容总条数

        /// </summary>
        public int Count
        {
            get { return ViewState["Count"] != null ? (int)ViewState["Count"] : 0; }
            set { ViewState["Count"] = value; }
        }
        #endregion

        #region = 2 PageNum =
        /// <summary>
        /// 每页内容数

        /// </summary>
        public int PageNum
        {
            get { return ViewState["PageNum"] != null ? (int)ViewState["PageNum"] : 20; }
            set { ViewState["PageNum"] = value; }
        }
        #endregion

        #region = 3 CurrentPageIndex =
        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPageIndex
        {
            get
            {
                int currentpageindex = 1;
                if (ViewState["CurrentPageIndex"] != null)
                {
                    int.TryParse(ViewState["CurrentPageIndex"].ToString(), out currentpageindex);
                }
                else
                {
                    if (this.Page.Request.QueryString["pages"] != null)
                    {
                        int.TryParse(this.Page.Request.QueryString["pages"].ToString(), out currentpageindex);
                    }
                }
                currentpageindex = currentpageindex < 1 ? 1 : currentpageindex;
                currentpageindex = currentpageindex > PageCount ? PageCount : currentpageindex;
                return currentpageindex;
            }
            set { ViewState["CurrentPageIndex"] = value; }
        }
        #endregion

        #region = 4 PageCount =
        /// <summary>
        /// 总页数

        /// </summary>
        public int PageCount
        {
            get { return (Count / PageNum) + ((Count % PageNum) == 0 ? 0 : 1); }
        }
        #endregion

        #region = 5 CssUrl =
        /// <summary>
        /// 分页样式
        /// </summary>
        public string CssUrl
        {
            get { return ViewState["CssUrl"] != null ? ViewState["CssUrl"].ToString() : this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "Common.Controls.Pagination.Pagination.css"); }
            set { ViewState["CssUrl"] = value; }
        }
        #endregion

        #region = 6 Mode =
        /// <summary>
        /// Pagination的模式

        /// </summary>
        public PaginationMode Mode
        {
            get
            {
                object obj = this.ViewState["Mode"];
                return ((obj == null) ? PaginationMode.Get : ((PaginationMode)obj));
            }
            set
            {
                this.ViewState["Mode"] = value;
            }
        }
        #endregion

        #region = 7 PagesName =
        /// <summary>
        /// 分页样式
        /// </summary>
        public string PagesName
        {
            get { return ViewState["PagesName"] != null ? ViewState["PagesName"].ToString() : "pages"; }
            set { ViewState["PagesName"] = value; }
        }
        #endregion

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (this.PageCount > 1)
            {
                if ((this.Page != null) && (this.Page.Header != null))
                {
                    string cssKey = "PaginationCss";
                    if (this.Page.Header.FindControl(cssKey) == null)
                    {
                        HtmlLink child = new HtmlLink();
                        child.ID = cssKey;
                        child.Href = CssUrl;
                        child.Attributes["type"] = "text/css";
                        child.Attributes["rel"] = "stylesheet";
                        this.Page.Header.Controls.Add(child);
                    }
                    this.CssClass = "pagenumber";
                }
            }
        }

        #region = RenderContents =
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            if (this.PageCount > 1)
            {
                writer.Write("共");
                writer.Write(Count);
                writer.Write("条");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("每页");
                writer.Write(PageNum);
                writer.Write("条");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("共");
                writer.Write(PageCount);
                writer.Write("页");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("当前为第");
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "f90");
                writer.RenderBeginTag(HtmlTextWriterTag.B);
                writer.Write(CurrentPageIndex);
                writer.RenderEndTag();
                writer.Write("页");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("选择页数:");

                for (int i = 1; i < 5; i++)
                {
                    if ((CurrentPageIndex - 5 + i) > 0)
                    {
                        if (this.Mode == PaginationMode.Get)
                        {
                            if (HttpContext.Current.Request.RawUrl.ToString().Contains(PagesName + "="))
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$1") + (CurrentPageIndex - 5 + i).ToString() + Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$3"));
                            }
                            else if (HttpContext.Current.Request.QueryString.Count != 0)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, HttpContext.Current.Request.RawUrl.ToString() + "&" + PagesName + "=" + (CurrentPageIndex - 5 + i).ToString());
                            }
                            else
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, "?" + PagesName + "=" + (CurrentPageIndex - 5 + i).ToString());
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex - 5 + i).ToString());
                            writer.RenderEndTag();
                        }
                        else
                        {
                            PostBackOptions postBackOptions = this.GetPostBackOptions(CurrentPageIndex - 5 + i);
                            string postBackEventReference = null;
                            if (postBackOptions != null)
                            {
                                postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(postBackOptions, true);
                            }
                            if (string.IsNullOrEmpty(postBackEventReference))
                            {
                                postBackEventReference = "javascript:void(0)";
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex - 5 + i).ToString());
                            writer.RenderEndTag();
                        }
                    }
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "current");
                writer.RenderBeginTag(HtmlTextWriterTag.B);
                writer.Write(CurrentPageIndex);
                writer.RenderEndTag();

                for (int i = 1; i < 5; i++)
                {
                    if ((CurrentPageIndex + i) <= PageCount)
                    {
                        if (this.Mode == PaginationMode.Get)
                        {
                            if (HttpContext.Current.Request.RawUrl.ToString().Contains(PagesName + "="))
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$1") + (CurrentPageIndex + i).ToString() + Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$3"));
                            }
                            else if (HttpContext.Current.Request.QueryString.Count != 0)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, HttpContext.Current.Request.RawUrl.ToString() + "&" + PagesName + "=" + (CurrentPageIndex + i).ToString());
                            }
                            else
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, "?" + PagesName + "=" + (CurrentPageIndex + i).ToString());
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex + i).ToString());
                            writer.RenderEndTag();
                        }
                        else
                        {
                            PostBackOptions postBackOptions = this.GetPostBackOptions(CurrentPageIndex + i);
                            string postBackEventReference = null;
                            if (postBackOptions != null)
                            {
                                postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(postBackOptions, true);
                            }
                            if (string.IsNullOrEmpty(postBackEventReference))
                            {
                                postBackEventReference = "javascript:void(0)";
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex + i).ToString());
                            writer.RenderEndTag();
                        }
                    }
                }

                if ((CurrentPageIndex + 4) < PageCount)
                {
                    writer.Write("...");
                }

                if (this.Mode == PaginationMode.Get)
                {
                    writer.Write("&nbsp;&nbsp;");
                    writer.Write("跳转到第");
                    writer.Write("<input id=\"" + this.UniqueID + "\" type=\"text\" onkeypress=\"if(event.keyCode==13){location.href='");
                    if (HttpContext.Current.Request.RawUrl.ToString().Contains("pages="))
                    {
                        writer.Write(Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)([\\w\\W^&]*)(.*)", "$1") + "'+this.value+'" + Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)([\\w\\W^&]*)(.*)", "$3") + "'");
                    }
                    else if (HttpContext.Current.Request.QueryString.Count != 0)
                    {
                        writer.Write(HttpContext.Current.Request.RawUrl.ToString() + "&" + PagesName + "='+this.value");
                    }
                    else
                    {
                        writer.Write(HttpContext.Current.Request.RawUrl.ToString() + "?" + PagesName + "='+this.value");
                    }
                    writer.Write(";return false;}\" class=\"page_input\"/>");
                    writer.Write("页");
                }
            }
        }
        #endregion

        private PostBackOptions GetPostBackOptions(int index)
        {
            PostBackOptions options = new PostBackOptions(this, index.ToString());
            options.RequiresJavaScriptProtocol = true;
            return options;
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            int num;
            if (int.TryParse(eventArgument, out num))
            {
                this.CurrentPageIndex = num;
            }
        }
    }
}
